/*
 * Copyright Visionmax Solutions Inc. 2003-2008
 */
package com.gs2.servlet;

import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;
import java.util.StringTokenizer;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailCenter
{
	/**
	 * The properties file
	 */
	private static SmtpAuthenticator authenticator = null;

	/**
	 * The properties file
	 */
	private static Properties props = null;

	/**
	 * The mail session
	 */
	private static Session mailSession = null;

	/**
	 * Email address the mail will send to
	 */
	private String to = "";

	/**
	 * Email address the mail will send to
	 */
	private String cc = "";

	/**
	 * Email address that the e-mail is from
	 */
	private String from = "test@nomail.com";

	/**
	 * User name for e-mail
	 */
	private String user = "E-mail Service";

	/**
	 * User name for e-mail
	 */
	private String password = "";

	/**
	 * User mime-type for e-mail
	 */
	private String mimeType = "text";

	/**
	 * Constructor for MailCenter
	 */
	public MailCenter(String smtpHost)
	{
		super();
		init(smtpHost);
	}

	/**
	 * Constructor for MailCenter
	 */
	public MailCenter(String smtpHost, String to)
	{
		super();
		init(smtpHost);
		this.to = to;
	}

	/**
	 * Constructor for MailCenter
	 */
	public MailCenter(String smtpHost, String to, String from)
	{
		super();
		init(smtpHost);
		this.to = to;
		this.from = from;
	}

	/**
	 * Constructor for MailCenter
	 */
	public MailCenter(String smtpHost, String to, String from, String user)
	{
		super();
		init(smtpHost);
		this.to = to;
		this.from = from;
		this.user = user;
	}
	/**
	 * Constructor for MailCenter
	 */
	public MailCenter(
		String smtpHost,
		String to,
		String from,
		String user,
		String password)
	{
		super();
		this.user = user;
		this.password = password;
		init(smtpHost);
		this.to = to;
		this.from = from;
	}

	/**
	 * Constructor for MailCenter
	 */
	public MailCenter(
		String smtpHost,
		String to,
		String from,
		String user,
		String password,
		String cc)
	{
		super();
		this.user = user;
		this.password = password;
		init(smtpHost);
		this.to = to;
		this.from = from;
		this.cc = cc;
	}

	/**
	 * Initialize the properties
	 * 
	 * @param smtpHost the host to use for mailing
	 */
	private void init(String smtpHost)
	{
		if (!this.password.equals(""))
			authenticator = new SmtpAuthenticator(user, password);
		if (props == null)
		{
			props = new Properties();
			props.put("mail.user", user);
			props.put("mail.smtp.host", smtpHost);
			if (authenticator != null)
				props.put("mail.smtp.auth", "true");
		}
		if (mailSession == null)
		{
			//mailSession = Session.getDefaultInstance(props, authenticator);
			mailSession = Session.getInstance(props, authenticator);
		}

	}

	/**
	 * Send email
	 * 
	 * @param to Target email address
	 * @param subject Email subject
	 * @param msgText Email body
	 * @param attachFileDir Attachment file dir
	 * @param attachFileDir Attachment file name
	 * 
	 * @exception AddressException Exception thrown when send email
	 * @exception MessagingException Exception thrown when send email
	 */
	public void sendMail(
		String to,
		String subject,
		String msgText,
		String attachFileDir,
		String attachFileName)
		throws AddressException, MessagingException
	{
		this.to = to;
		sendMail(subject, msgText, attachFileDir, attachFileName);
	}

	/**
	 * Send email
	 * 
	 * @param subject Email subject
	 * @param msgText Email body
	 * @param attachFileDir Attachment file dir
	 * @param attachFileDir Attachment file name
	 * 
	 * @exception AddressException Exception thrown when send email
	 * @exception MessagingException Exception thrown when send email
	 */
	public void sendMail(
		String subject,
		String msgText,
		String attachFileDir,
		String attachFileName)
		throws AddressException, MessagingException
	{
		Message msg = new MimeMessage(mailSession);
		InternetAddress[] toAddress = toAddressList(to);
		InternetAddress[] ccAddress = toAddressList(cc);
		msg.setRecipients(Message.RecipientType.TO, toAddress);
		msg.setRecipients(Message.RecipientType.CC, ccAddress);
		msg.setSubject(subject);
		msg.setSentDate(new Date());
		msg.setFrom(new InternetAddress(from));

		// Create the message part 
		BodyPart messageBodyPart = new MimeBodyPart();

		// Fill the message
		messageBodyPart.setContent(msgText, mimeType);

		Multipart multipart = new MimeMultipart();
		multipart.addBodyPart(messageBodyPart);

		if (attachFileName != null && attachFileDir != null)
		{
			// Part two is attachment
			messageBodyPart = new MimeBodyPart();
			DataSource source =
				new FileDataSource(attachFileDir + attachFileName);
			messageBodyPart.setDataHandler(new DataHandler(source));
			messageBodyPart.setFileName(attachFileName);
			multipart.addBodyPart(messageBodyPart);
		}

		// Put parts in message
		msg.setContent(multipart);

		Transport.send(msg);
	}
	/**
	 * Returns the mimeType.
	 * @return String
	 */
	public String getMimeType()
	{
		return mimeType;
	}

	/**
	 * Sets the mimeType.
	 * @param mimeType The mimeType to set
	 */
	public void setMimeType(String mimeType)
	{
		this.mimeType = mimeType;
	}

	public InternetAddress[] toAddressList(String string)
		throws AddressException
	{
		StringTokenizer tok = new StringTokenizer(string, ",; ");
		ArrayList list = new ArrayList();
		while (tok.hasMoreTokens())
			list.add(new InternetAddress(tok.nextToken()));

		InternetAddress[] addresses = new InternetAddress[list.size()];
		addresses = (InternetAddress[])list.toArray(addresses);
		return addresses;
	}

}
